package com.lwy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwy.dao.BookMessageMapper;
import com.lwy.entity.BookMessage;
import com.lwy.entity.BookNove;
import com.lwy.entity.BookNovelChapter;
import com.lwy.service.BookMessageService;
import com.lwy.util.ChapterFilter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

//消息表service接口实现类
@Service
public class BookMessageServiceImpl extends ServiceImpl<BookMessageMapper, BookMessage> implements BookMessageService {

    @Resource
    private BookNoveServiceImpl bookNoveService;
    @Resource
    private BookNovelChapterServiceImpl bookNovelChapterService;


    public List<BookMessage> listUserId(Integer uid){
        List<BookMessage> uid1 = baseMapper.selectList(new QueryWrapper<BookMessage>().eq("uid", uid).eq("status",1));
        for (BookMessage bookMessage : uid1) {
            List<BookNove> byList1 = bookNoveService.findByList(bookMessage.getNovel_id());
            BookNovelChapter byId = bookNovelChapterService.getById(bookMessage.getChapter_id());
            ChapterFilter.setEntity(byId);
            bookMessage.setBookNove(byList1);
            bookMessage.setBookNovelChapter(byId);
        }
        return uid1;
    }

    public Object clear(Integer uid) {
        int insert = 0;
        List<BookMessage> uid1 = baseMapper.selectList(new QueryWrapper<BookMessage>().eq("uid", uid).eq("status",1));
        Integer status = 0;
        for (BookMessage bookMessage : uid1) {
            BookMessage bookMessage1 = new BookMessage();
            bookMessage1.setId(bookMessage.getId());
            bookMessage1.setStatus(status);
            insert+=baseMapper.updateById(bookMessage1);
        }
        if(insert>0){
            return 1;
        }else{
            return 0;
        }
    }
}
